package de.fhluebeck.oop.gps.cmd;

import java.util.HashMap;
import java.util.Map;

import de.fhluebeck.oop.gps.common.Constants;
import de.fhluebeck.oop.gps.common.DependencyManager;
import de.fhluebeck.oop.gps.service.IDatabaseService;

/**
 * 
 * Stellt den Einstiegspunkt für das CoR Pattern dar. 
 * Eingabe Parameter werden geparsed und der Reihe nach an die Chain weitergereicht
 * 
 * @author Ogün Bilge, Leif Hitzschke
 * @version 1.00, 13.11.2009
 * 
 */
public class Command {

	private Option chain;
	
	private Command() {
		chain = new ExportWebserviceCommand(new CreateDummyDataOption(new ShowPoiOption(new HelpOption())));
		((IDatabaseService) DependencyManager
				.locate(Constants.DATABASE_SERVICE_DEP_NAME)).initDatabase();
	}

	private Map<String, String> parse(String[] args) {
		Map<String, String> options = new HashMap<String, String>();
		String currOpt = "";
		String currOptParam = "";
		for (String string : args) {
			if (string.startsWith("-")) {
				if (currOpt != null && currOpt.length() > 0) {
					options.put(currOpt, currOptParam);
					currOpt = "";
					currOptParam = "";
				}
				currOpt = string;
			} else {
				currOptParam += string;
			}
		}
		options.put(currOpt, currOptParam);
		return options;
	}

	public void execute(String[] args) {
		Map<String, String> options = parse(args);
		for (Map.Entry<String, String> entry : options.entrySet()) {
			chain.process(entry.getKey(), entry.getValue());
		}
	}
	public static void main(String[] args) {
		Command cmd = new Command();
		cmd.execute(args);
	}
}
